package controllers;

import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpSession;

import beans.UserBeanRemote;
import dto.DTOUser;


public class EditUserController {

	@EJB
	UserBeanRemote ubr;

	private DTOUser etUser;

	public DTOUser getEtUser() {
		return etUser;
	}

	public void setEtUser(DTOUser etUser) {
		this.etUser = etUser;
	}

	private String renderUpdateUser;

	public String getRenderUpdateUser() {
		return renderUpdateUser;
	}


	public void setRenderUpdateUser(String renderUpdateUser) {
		this.renderUpdateUser = renderUpdateUser;
	}

	public String updateOkRedirect() {
		renderUpdateUser = null;
		return "ViewUsers.xhtml?faces-redirect=true";
	}

	public String update(){
		// /// internationalizare
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session
				.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage() == "ro") {
			messageBundleName += "_ro";
		} else if (language.getLanguage() == "hu") {
			messageBundleName += "_hu";
		} else if (language.getLanguage() == "de") {
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		// ///
		try{
			ubr.updateUser(etUser);
			etUser=null;
			renderUpdateUser = messageBundle.getString("userUpdatedSucces");
		}catch(Exception e) {
			renderUpdateUser = messageBundle.getString("userUpdatedSucces");
		}
		return "ok";
	}

	public void attributeListener(ActionEvent event){ 
		Integer i = (Integer) event.getComponent().getAttributes().get("idUser");
		etUser = ubr.getUser(i);
	}

	public void validateName1(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

		// internationalization
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session
				.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage() == "ro") {
			messageBundleName += "_ro";
		} else if (language.getLanguage() == "hu") {
			messageBundleName += "_hu";
		} else if (language.getLanguage() == "de") {
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		// ////////

		String enteredName = (String) value;

		if ((ubr.existsNameInDB(enteredName))&&(!ubr.getUser(etUser.getId()).getName().equals(enteredName))) {
				FacesMessage message = new FacesMessage();
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
				message.setSummary(messageBundle.getString("usernameAlreadyExists"));
				message.setDetail(messageBundle.getString("usernameAlreadyExists"));
				throw new ValidatorException(message);
			}
	}
	
	public void validateEmail1(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

		// internationalization
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session
				.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage() == "ro") {
			messageBundleName += "_ro";
		} else if (language.getLanguage() == "hu") {
			messageBundleName += "_hu";
		} else if (language.getLanguage() == "de") {
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		// ////////

		String enteredName = (String) value;

		if ((ubr.existsEmailInDB(enteredName))&&(!ubr.getUser(etUser.getId()).getEmail().equals(enteredName))) {
				FacesMessage message = new FacesMessage();
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
				message.setSummary(messageBundle.getString("emailAlreadyInDatabase"));
				message.setDetail(messageBundle.getString("emailAlreadyInDatabase"));
				throw new ValidatorException(message);
			}
	}
}
